package com.haois.mp.database.module.basic.controller;

import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.constant.Reply;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * Created by RuiHaoZ on 2017/11/20.
 */
@RestController
public class BaseRestController {
    private Logger logger = LoggerFactory.getLogger(BaseRestController.class);

    @RequestMapping(value = "/api/doc", method = RequestMethod.GET)
    public ModelAndView getApiDoc(HttpServletRequest request) {
        String to = System.getProperty("os.name") + ":" + System.getProperty("os.version");
        String param = "to=" + to;
        return new ModelAndView(new RedirectView("http://api.haois.me/mp?" + param));
    }

    @RequestMapping(value = "/api", method = RequestMethod.GET)
    public String getApi(HttpServletRequest request) {
        return new Reply(ApiResultEnum.API_00).toJson();
    }

    public String getLocalHost() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            return address.getHostName() + ":" + address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return "获取失败";
        }
    }

}
